﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Security;
using System.Security.Principal;

namespace Business.AutheAuthor
{
    public class PrincipalPerm
    {



        public static void PrincipalPermisionDemo()
        {
            //DeclarativeRBS();
            ImperativeRBS();
        }

        private static void ImperativeRBS()
        {
            AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

            try
            {
                PrincipalPermission p = new PrincipalPermission(@"mita-PC\mita", @"BUILTIN\Administrators", true);
                p.Demand();


                //main code here
            }
            catch (SecurityException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        #region Declaretive RBS
        private static void DeclarativeRBS()
        {
            AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

            try
            {
                AdministratorsOnlyMehod();
            }
            catch (SecurityException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        [PrincipalPermission(SecurityAction.Demand, Name = @"mita-PC\mita", Role = @"BUILTIN\Administrators")]
        private static void AdministratorsOnlyMehod()
        {
            Console.WriteLine("You are administrator !");
        } 
        #endregion
    }
}
